package ${projectConfig.basePackage}.${config.model}.dao.gen;

import java.util.List;
import java.util.Map;
import java.util.Date;
import java.math.BigDecimal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import ${projectConfig.commPackage}.bean.PageBean;
import ${projectConfig.commPackage}.util.TypeCast;
import ${projectConfig.commPackage}.exception.ServiceException;
import ${projectConfig.beanPackage}.${config.model}.bean.po.gen.${config.objectName};
import ${projectConfig.basePackage}.${config.model}.mapper.gen.${config.objectName}Mapper;
import ${projectConfig.beanPackage}.${config.model}.bean.po.gen.${config.objectName}Example;
@Repository
public class ${config.objectName}Dao {
	// 日志对象
	private transient Log log = LogFactory.getLog(this.getClass());
	@Autowired
	private ${config.objectName}Mapper ${config.objectName?uncap_first}Mapper;

	// 增加一个后台${config.objectName}表
	
	public boolean insert(${config.objectName} ${config.objectName?uncap_first}) {
		try {
${config.objectName?uncap_first}Mapper.insert(${config.objectName?uncap_first});
			log.debug("后台${config.objectName}表增加成功");
			return true;
		} catch (Exception e) {
			log.debug("后台${config.objectName}表增加失败");
			throw new ServiceException("后台${config.objectName}表增加失败",e);
		}
	}
	// 增加一个后台${config.objectName}表Selective
	
	public boolean insertSelective(${config.objectName} ${config.objectName?uncap_first}) {
		try {
${config.objectName?uncap_first}Mapper.insertSelective(${config.objectName?uncap_first});
			log.debug("后台${config.objectName}表增加成功");
			return true;
		} catch (Exception e) {
			log.debug("后台${config.objectName}表增加失败");
			throw new ServiceException("后台${config.objectName}表增加失败",e);
		}
	}

	// 删除一个后台${config.objectName}表
	
	public boolean deleteByPrimaryKey(<#list pks as prop>${prop.javaType} ${prop.alias}<#if prop_has_next>,</#if></#list>) {
		try{
${config.objectName?uncap_first}Mapper.deleteByPrimaryKey(<#list pks as prop>${prop.alias}<#if prop_has_next>,</#if></#list>);
			log.debug("后台${config.objectName}表删除成功");
			return true;
		} catch(Exception e){
			log.debug("后台${config.objectName}表删除失败");
			throw new ServiceException("后台${config.objectName}表删除失败",e);
		}
	}
	// 删除一个后台${config.objectName}表byMap
	
	public boolean deleteByMap(Map ${config.objectName?uncap_first}Map) {
		try{
${config.objectName?uncap_first}Mapper.deleteByExample(create${config.objectName}Example(${config.objectName?uncap_first}Map,null,null));
			log.debug("后台${config.objectName}根据Map删除成功");
			return true;
		}
		
		catch(Exception e){
			log.debug("后台${config.objectName}根据GroupID删除失败");
			throw new ServiceException("后台${config.objectName}根据object删除失败",e);
		}
	}
	// 修改一个后台${config.objectName}表
	
	public boolean updateByPrimaryKey(${config.objectName} ${config.objectName?uncap_first}) {
		try{
${config.objectName?uncap_first}Mapper.updateByPrimaryKey(${config.objectName?uncap_first});
			log.debug("后台${config.objectName}表修改成功");
			return true;
		}
		
		catch(Exception e){
			log.debug("后台${config.objectName}表修改失败");
			throw new ServiceException("后台${config.objectName}表修改失败",e);
		}
	}
	// 修改一个后台${config.objectName}表Selective
	
	public boolean updateByPrimaryKeySelective(${config.objectName} ${config.objectName?uncap_first}) {
		try{
${config.objectName?uncap_first}Mapper.updateByPrimaryKeySelective(${config.objectName?uncap_first});
			log.debug("后台${config.objectName}表修改成功");
			return true;
		}
		
		catch(Exception e){
			log.debug("后台${config.objectName}表修改失败");
			throw new ServiceException("后台${config.objectName}表修改失败",e);
		}
	}
	// 修改一个后台${config.objectName}表
	
	public boolean updateByMap(${config.objectName} ${config.objectName?uncap_first},Map ${config.objectName?uncap_first}Map) {
		try{
${config.objectName?uncap_first}Mapper.updateByExample(${config.objectName?uncap_first},create${config.objectName}Example(${config.objectName?uncap_first}Map,null,null));
			log.debug("后台批量${config.objectName}表修改成功");
			return true;
		}
		catch(Exception e){
			log.debug("后台批量${config.objectName}表修改失败");
			throw new ServiceException("后台${config.objectName}表批量修改失败",e);
		}
	}
	// 修改一个后台${config.objectName}表Selective
	
	public boolean updateByMapSelective(${config.objectName} ${config.objectName?uncap_first},Map ${config.objectName?uncap_first}Map) {
		try{
${config.objectName?uncap_first}Mapper.updateByExampleSelective(${config.objectName?uncap_first},create${config.objectName}Example(${config.objectName?uncap_first}Map,null,null));
			log.debug("后台批量${config.objectName}表修改成功");
			return true;
		}
		catch(Exception e){
			log.debug("后台批量${config.objectName}表修改失败");
			throw new ServiceException("后台${config.objectName}表批量修改失败",e);
		}
	}

	// 查询一个后台${config.objectName}表
	
	public ${config.objectName} selectByPrimaryKey(<#list pks as prop>${prop.javaType} ${prop.alias}<#if prop_has_next>,</#if></#list>) {
		return ${config.objectName?uncap_first}Mapper.selectByPrimaryKey(<#list pks as prop>${prop.alias}<#if prop_has_next>,</#if></#list>);
	}
	
	// 查询一个后台${config.objectName}表-根据map
	
	public ${config.objectName} selectByMap(Map ${config.objectName?uncap_first}Map) {
		List<${config.objectName}> list = getListByMap(${config.objectName?uncap_first}Map);
		if(list==null||list.isEmpty()){
			return null;
		}else{
			return list.get(0);
		}
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////////
	// 查询所有后台${config.objectName}表
	
	public List<${config.objectName}> getListByMap(Map ${config.objectName?uncap_first}Map) {
		
		return ${config.objectName?uncap_first}Mapper.selectByExample(create${config.objectName}Example(${config.objectName?uncap_first}Map,null,null));
	}

	//统计后台${config.objectName}表数量
	
	public int countTotalByMap(Map ${config.objectName?uncap_first}Map) {
		
		return ${config.objectName?uncap_first}Mapper.countByExample(create${config.objectName}Example(${config.objectName?uncap_first}Map,null,null));
	}

	
	public List<${config.objectName}> getListByMapPage(Map ${config.objectName?uncap_first}Map, int limitStart,
			int limitOffset) {
		
		return ${config.objectName?uncap_first}Mapper.selectByExample(create${config.objectName}Example(${config.objectName?uncap_first}Map,limitStart,limitOffset));
	}
	
	
	public PageBean getPageBeanByMap(Map ${config.objectName?uncap_first}Map,int pageNo,int pageSize) {
		Integer count = countTotalByMap(${config.objectName?uncap_first}Map);
		PageBean pageBean = new PageBean();
		pageBean.setCount(count);
		pageBean.setPageNo(pageNo);
		pageBean.setPageSize(pageSize);
		int limitStart = pageBean.getStartNo();
		int limitOffset = pageSize;
		pageBean.setData(${config.objectName?uncap_first}Mapper.selectByExample(create${config.objectName}Example(${config.objectName?uncap_first}Map,limitStart,limitOffset)));
		return pageBean;
	}
		
	private ${config.objectName}Example create${config.objectName}Example(Map ${config.objectName?uncap_first}Map,Integer limitStart,Integer limitOffset){
${config.objectName}Example ${config.objectName?uncap_first}Ex = new ${config.objectName}Example();
${config.objectName}Example.Criteria c = ${config.objectName?uncap_first}Ex.createCriteria();
<#list allcolumns as prop>
    ${prop.javaType} ${prop.alias}_null = TypeCast.get${prop.javaType}(${config.objectName?uncap_first}Map.get("${prop.alias}_null"));
    ${prop.javaType} ${prop.alias}_notNull = TypeCast.get${prop.javaType}(${config.objectName?uncap_first}Map.get("${prop.alias}_notNull"));
    ${prop.javaType} ${prop.alias} = TypeCast.get${prop.javaType}(${config.objectName?uncap_first}Map.get("${prop.alias}"));
    ${prop.javaType} ${prop.alias}_not = TypeCast.get${prop.javaType}(${config.objectName?uncap_first}Map.get("${prop.alias}_not"));
    ${prop.javaType} ${prop.alias}_greater = TypeCast.get${prop.javaType}(${config.objectName?uncap_first}Map.get("${prop.alias}_greater"));
    ${prop.javaType} ${prop.alias}_greaterEqual = TypeCast.get${prop.javaType}(${config.objectName?uncap_first}Map.get("${prop.alias}_greaterEqual"));
    ${prop.javaType} ${prop.alias}_less = TypeCast.get${prop.javaType}(${config.objectName?uncap_first}Map.get("${prop.alias}_less"));
    ${prop.javaType} ${prop.alias}_lessEqual = TypeCast.get${prop.javaType}(${config.objectName?uncap_first}Map.get("${prop.alias}_lessEqual"));
    <#if prop.javaType == "String">
        ${prop.javaType} ${prop.alias}_like = TypeCast.get${prop.javaType}(${config.objectName?uncap_first}Map.get("${prop.alias}_like"));
        ${prop.javaType} ${prop.alias}_notLike = TypeCast.get${prop.javaType}(${config.objectName?uncap_first}Map.get("${prop.alias}_notLike"));
    </#if>
				List<${prop.javaType}> ${prop.alias}_in = TypeCast.get${prop.javaType}List(${config.objectName?uncap_first}Map.get("${prop.alias}_in"));
				List<${prop.javaType}> ${prop.alias}_notIn = TypeCast.get${prop.javaType}List(${config.objectName?uncap_first}Map.get("${prop.alias}_notIn"));
    ${prop.javaType} ${prop.alias}_between1 = TypeCast.get${prop.javaType}(${config.objectName?uncap_first}Map.get("${prop.alias}_between1"));
    ${prop.javaType} ${prop.alias}_between2 = TypeCast.get${prop.javaType}(${config.objectName?uncap_first}Map.get("${prop.alias}_between2"));
    ${prop.javaType} ${prop.alias}_notBetween1 = TypeCast.get${prop.javaType}(${config.objectName?uncap_first}Map.get("${prop.alias}_notBetween1"));
    ${prop.javaType} ${prop.alias}_notBetween2 = TypeCast.get${prop.javaType}(${config.objectName?uncap_first}Map.get("${prop.alias}_notBetween2"));
				
				if(${prop.alias}_null != null){
					c.and${prop.alias?cap_first}IsNull();
				}
				if(${prop.alias}_notNull != null){
					c.and${prop.alias?cap_first}IsNotNull();
				}
				if(${prop.alias} != null){
					c.and${prop.alias?cap_first}EqualTo(${prop.alias});
				}
				if(${prop.alias}_not != null){
					c.and${prop.alias?cap_first}NotEqualTo(${prop.alias}_not);
				}
				if(${prop.alias}_greater != null){
					c.and${prop.alias?cap_first}GreaterThan(${prop.alias}_greater);
				}
				if(${prop.alias}_greaterEqual != null){
					c.and${prop.alias?cap_first}GreaterThanOrEqualTo(${prop.alias}_greaterEqual);
				}
				if(${prop.alias}_less != null){
					c.and${prop.alias?cap_first}LessThan(${prop.alias}_less);
				}
				if(${prop.alias}_lessEqual != null){
					c.and${prop.alias?cap_first}LessThanOrEqualTo(${prop.alias}_lessEqual);
				}
    <#if prop.javaType == "String">
				if(${prop.alias}_like != null){
					c.and${prop.alias?cap_first}Like(${prop.alias}_like);
				}
				if(${prop.alias}_notLike != null){
					c.and${prop.alias?cap_first}NotLike(${prop.alias}_notLike);
				}
    </#if>
				if(${prop.alias}_in != null){
					c.and${prop.alias?cap_first}In(${prop.alias}_in);
				}
				if(${prop.alias}_notIn != null){
					c.and${prop.alias?cap_first}NotIn(${prop.alias}_notIn);
				}
				if(${prop.alias}_between1 != null){
					c.and${prop.alias?cap_first}Between(${prop.alias}_between1,${prop.alias}_between2);
				}
				if(${prop.alias}_notBetween1 != null){
					c.and${prop.alias?cap_first}NotBetween(${prop.alias}_notBetween1,${prop.alias}_notBetween2);
				}
</#list>
<#list columns as prop>
    <#if prop.name == "sortOrder">
        ${config.objectName?uncap_first}Ex.setOrderByClause("sort_order");
    </#if>
</#list>
			if(${config.objectName?uncap_first}Map.get("orderBy")!=null){
${config.objectName?uncap_first}Ex.setOrderByClause((String)${config.objectName?uncap_first}Map.get("orderBy"));
			}
			if(limitStart != null && limitOffset != null){
${config.objectName?uncap_first}Ex.setLimitStart(limitStart);
${config.objectName?uncap_first}Ex.setLimitOffset(limitOffset);
			}
			
			return ${config.objectName?uncap_first}Ex;
	}
}